package controllers;

import helpers.GeneralHelper;
import helpers.StringHelper;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.cache.Cache;
import javax.script.Bindings;
import javax.script.CompiledScript;
import javax.script.ScriptContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;

import quercus.CompileScriptEngine;

import systems.Recently;
import systems.RecentlyTag;

import models.ArticleModel;
import models.CategoryModel;
import models.UserModel;

import entities.ApplicationConfig;
import entities.Article;
import entities.Category;
import entities.User;

public class ArticleInfoServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		ApplicationConfig appConfig = (ApplicationConfig) request.getAttribute("sysConfig");
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String alias = URLDecoder.decode(path.replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		String language = (String) request.getAttribute("language");
		if (language == null || language.isEmpty()) {
			language = "en";
		}
		
		ArticleModel articleModel = ArticleModel.getInstance();
		Article article = articleModel.getArticleByAlias(appConfig.getApplicationId(), alias, language);
		
		if (article == null) {
			response.sendRedirect("/msg/page-not-found");
			return;
		}
		
		if (article.getStatus() == 0) {
			response.sendRedirect("/msg/page-not-found");
			return;
		}
		
		CategoryModel categoryModel = CategoryModel.getInstance();
		Category category = categoryModel.getCategory(appConfig.getApplicationId(), article.getCategory());
		
		//Get related by category
		List<Article> cArticles = articleModel.getRelatedByCategory(appConfig.getApplicationId(), article.getCategory(), language);
		
		//Get author
		UserModel userModel = UserModel.getInstance();
		User author = null;
		if (!article.getCreator().isEmpty()) {
			author = userModel.getUser(appConfig.getApplicationId(), article.getCreator());
		}
		
		//Get related by author
		List<Article> uArticles = new ArrayList<Article>();
		if (author != null) {
			uArticles = articleModel.getRelatedByAuthor(appConfig.getApplicationId(), article.getCreator(), language);
		}
		
		
		Recently.update(appConfig.getApplicationId(), article);
		
		
		request.setAttribute("article", article);
		request.setAttribute("cArticles", cArticles);
		request.setAttribute("uArticles", uArticles);
		request.setAttribute("author", author);
		request.setAttribute("category", category);
		try {
			request.getRequestDispatcher("/templates/" + appConfig.getTemplateId() + "/pages/article_info.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
		
	}
}
